#!/usr/bin/python

# nextbussh.py: A shell for grabbing low-level NextBus API info
# $Id$

# Copyright 2011 Portland Transport

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Authors:
# Matt Conway: main code

from nextbus import *

print "You can enter 'list' to get a list of options for any command"
a = raw_input('Agency: ')

if a == 'list':
    print 'Agency'.ljust(55), 'Tag'
    for i in get_all_agencies():
        print i.title.ljust(55), i.tag
    exit()

l = raw_input('Route: ')
if l == 'list':
    print 'Route'.ljust(10), 'Name'
    for i in get_all_routes_for_agency(a):
        print str(i.tag).ljust(10), str(i.title)
    exit()

g = get_route_config(a, l)
print 'Selected %s route %s' % (a, g.route.title)
while 1:
    cmd = raw_input('NextBus (%s %s) > ' % (a, g.route.tag))
    if cmd == 'list':
        print """Commands:
stops             List stops for the selected route
stop              Detailed info about a stop
predictions       Get predictions for the route. Prompts for stop id.
exit              Exit the program
"""
    
    elif cmd == 'stops':
        print 'ID'.ljust(10), 'Stop'.ljust(40), 'Tag'
        for i in g.stops:
            print str(i.stop_id).ljust(10), str(i.title).ljust(50), i.tag
    
    elif cmd == 'stop':
        sid = raw_input('Stop ID: ')
        s = g.stops_dict[sid]
        print """
Stop Info for Stop {s.title}
ID       : {s.stop_id}
Tag      : {s.tag}
Name     : {s.title}
Direction: {s.direction_tag}
Latitude : {s.latitude}
Longitude: {s.longitude}
""".format(s = s)

    elif cmd == 'predictions':
        s = raw_input('Stop ID: ')
        for i in get_predictions_for_stop(a, s).predictions:
            print str(i.minutes).rjust(2) + ' min', i.direction.route.title, i.direction.title

    elif cmd == 'exit':
        break
        
    else:
        print '%s: No match' % cmd
